/**
 * @file: WeighingRecordServiceImpl
 * @author: michael
 * @date: 2017/8/10 10:28
 * @copyright: 南京凯盛
 */
package cn.com.kisen.kdc.service.weighing.cmt;

import cn.com.kisen.kdc.domain.StatisData;
import cn.com.kisen.kdc.domain.weighing.SimpleWeighingRecord;
import cn.com.kisen.kdc.mapper.weighing.cmt.CmtMapper;
import com.kisen.common.core.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 *
 *
 * @author michael
 * @version 1.0 Created on 2017/8/10 10:28
 */
@Service
public class CMTStandardServiceImpl implements CMTStandardService{

    @Autowired
    private CmtMapper cmtMapper;

    @Override
    public List<StatisData> sumByday(int inout, String materialName, long startTime, long endTime){
        Map<String, Object> param = new HashMap<>();
        if (inout == 0){
            param.put("inout", "采购");
        }else {
            param.put("inout", "销售");
        }
        param.put("materialName", materialName);
        param.put("startTime", DateUtil.millis2FormatTime(DateUtil.getDayStart(startTime)));
        param.put("endTime", DateUtil.millis2FormatTime(DateUtil.getDayEnd(endTime)));
        return cmtMapper.sumByDay(param);
    }

    @Override
    public List<StatisData> sum(int inout, String materialName, int field, Date dateTime) {
        Map<String, Object> param = new HashMap<>();
        if (inout == 0){
            param.put("inout", "采购");
        }else {
            param.put("inout", "销售");
        }
        param.put("materialName", materialName);

        String startTime;
        String endTime;
        Calendar cal = Calendar.getInstance();
        cal.setTime(dateTime);
        if (field == 2) {
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MINUTE, 0);
            startTime = DateUtil.calendar2FormatTime(cal);
            cal.set(Calendar.HOUR_OF_DAY, 23);
            cal.set(Calendar.SECOND, 59);
            cal.set(Calendar.MINUTE, 59);
            endTime = DateUtil.calendar2FormatTime(cal);
        } else if (field == 3) {
            cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
            cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
            startTime = DateUtil.calendar2FormatTime(cal);
            cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
            cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
            endTime = DateUtil.calendar2FormatTime(cal);
        } else if (field == 4) {
            cal.set(cal.get(Calendar.YEAR), Calendar.JANUARY, cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
            cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
            startTime = DateUtil.calendar2FormatTime(cal);
            cal.add(Calendar.YEAR, 1);
            endTime = DateUtil.calendar2FormatTime(cal);
        } else {
            return null;
        }
        param.put("startTime", startTime);
        param.put("endTime", endTime);
        return cmtMapper.sumBy(param);
    }

    @Override
    public List<SimpleWeighingRecord> getTruck(int inout, String startTime, String endTime) {
        Map<String, Object> param = new HashMap<>();
        param.put("inout", inout);
        param.put("startTime", startTime);
        param.put("endTime", endTime);
        return cmtMapper.getTruck(param);
    }
}
